#include <algorithm>
#include <cstdio>
#include <cstring>

typedef long long ll;
const int N = 2e2 + 5;
int n, t, a[N], pre[N], pos[N];
ll f[N][N][N];

ll solve(int l, int r, int k) {
  if (l > r) return 0;
  if (f[l][r][k]) return f[l][r][k];
  f[l][r][k] = solve(l, r - 1, 0) + 1ll * (k + 1) * (k + 1);
  for (int h = pre[r]; h >= l; h = pre[h]) {
    f[l][r][k] =
        std::max(f[l][r][k], solve(l, h, k + 1) + solve(h + 1, r - 1, 0));
  }
  return f[l][r][k];
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("UVA10559 方块消除 Blocks.in", "r", stdin);
  freopen("UVA10559 方块消除 Blocks.out", "w", stdout);
#endif
#endif

  scanf("%d", &t);
  for (int i = 1; i <= t; ++i) {
    scanf("%d", &n);
    memset(f, 0, sizeof(f));
    memset(pos, 0, sizeof(pos));
    memset(pre, 0, sizeof(pre));
    for (int j = 1; j <= n; ++j) {
      scanf("%d", &a[j]), pre[j] = pos[a[j]], pos[a[j]] = j;
    }
    printf("Case %d: %lld\n", i, solve(1, n, 0));
  }
  return 0;
}